﻿using C1.Phone.FlexGrid;
using C1.Silverlight.Resources;
using SmartMobile.Data;
using SMARTMobile.Models.Local;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.Models;
using SMARTMobile.Models.UI;

namespace SMARTMobile.ViewModels
{
    public class OutletVendorContributionViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<OutltVendorContributionExtend> _outletVendorContributionObserable;    
  
        #endregion

        public OutletVendorContributionViewModel()
        {
            this.CreateConnection();
            this.GetOutltVendorContribution();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<OutltVendorContributionExtend> OutletVendorContributionObserable
        {
            get { return this._outletVendorContributionObserable; }
            set
            {
                this._outletVendorContributionObserable = value;
                base.RaisePropertyChanged("OutletVendorContributionObserable");
            }
        }

        #endregion

        #region Command
        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion

        #region Command Action Handlers
        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                this._smartMobileContext.OutletVendorContribution.DeleteAllOnSubmit(this._smartMobileContext.OutletVendorContribution.Where(x => x.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList());             

                this._smartMobileContext.SubmitChanges();
            }

            List<OutletVendorContribution> outletVenderList = new List<OutletVendorContribution>();
            double? sum = 0;
            foreach (var outletVendorContribution in this.OutletVendorContributionObserable)
            {
                OutletVendorContribution vender = new OutletVendorContribution
                {
                    ID_PK = outletVendorContribution.ID_PK,
                    CustomerKey = outletVendorContribution.CustomerKey,
                    OutletVendorKey = outletVendorContribution.OutletVendorKey,
                    ContributionAmt = outletVendorContribution.ContributionAmt
                };
                outletVenderList.Add(vender);
                sum += outletVendorContribution.ContributionAmt;
            }

            if (sum != 100)
            {
                this.ShowMessage("Tỉ lệ chưa đúng 100%");
                this.SaveCommand.RaiseCanExecuteChanged(true);
                return;
            }

            this._smartMobileContext.OutletVendorContribution.InsertAllOnSubmit(outletVenderList);
            this._smartMobileContext.SubmitChanges();

            this.IsBusy = false;

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method
       
        private void GetOutltVendorContribution()
        {
            this.OutletVendorContributionObserable = new ObservableCollection<OutltVendorContributionExtend>();

            List<OutletVendor> outletVenders = this._smartMobileContext.OutletVendor.ToList();
            if (outletVenders.Count == 0)
            {
                return;
            }

            foreach (var item in outletVenders)
            {
                OutltVendorContributionExtend outlet = new OutltVendorContributionExtend
                {
                    ID_PK = Guid.NewGuid(),
                    CustomerKey = GlobalInformation.SelectedTCARCUS.Customerkey,
                    OutletVendorKey = item.OutletVendorKey,
                    OutletVendorName = item.OutletVendorName,
                    ContributionAmt = 0
                };

                this.OutletVendorContributionObserable.Add(outlet);
            }

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<OutletVendorContribution> outletVendorContribution = this._smartMobileContext.OutletVendorContribution.Where(o => o.CustomerKey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                if (outletVendorContribution.Count > 0)
                {
                    if (this.OutletVendorContributionObserable.Count == outletVendorContribution.Count)
                    {
                        this.OutletVendorContributionObserable = new ObservableCollection<OutltVendorContributionExtend>();
                        foreach (var item in outletVendorContribution)
                        {
                            OutltVendorContributionExtend outlet = new OutltVendorContributionExtend
                            {
                                ID_PK = item.ID_PK,
                                CustomerKey = item.CustomerKey,
                                OutletVendorKey = item.OutletVendorKey,
                                OutletVendorName = this.GetOutletVendorName(item.OutletVendorKey),
                                ContributionAmt = item.ContributionAmt
                            };

                            this.OutletVendorContributionObserable.Add(outlet);
                        }
                    }
                    else
                    {
                        foreach (var item in outletVendorContribution)
                        {
                            OutltVendorContributionExtend selectedItem = this.OutletVendorContributionObserable.FirstOrDefault(o => (o.CustomerKey == item.CustomerKey && o.OutletVendorKey == item.CustomerKey));

                            if (selectedItem != null)
                            {
                                selectedItem.ID_PK = item.ID_PK;
                                selectedItem.CustomerKey = item.CustomerKey;
                                selectedItem.OutletVendorKey = item.OutletVendorKey;
                                selectedItem.OutletVendorName = this.GetOutletVendorName(item.OutletVendorKey);
                                selectedItem.ContributionAmt = item.ContributionAmt;
                            }                            
                        }

                        this._smartMobileContext.OutletVendorContribution.DeleteAllOnSubmit(this._smartMobileContext.OutletVendorContribution);
                        this._smartMobileContext.SubmitChanges();

                        List<OutletVendorContribution> outletVenderList = new List<OutletVendorContribution>();                      
                        foreach (var item in this.OutletVendorContributionObserable)
                        {
                            OutletVendorContribution vender = new OutletVendorContribution
                            {
                                ID_PK = item.ID_PK,
                                CustomerKey = item.CustomerKey,
                                OutletVendorKey = item.OutletVendorKey,
                                ContributionAmt = item.ContributionAmt
                            };
                            outletVenderList.Add(vender);                         
                        }
                        
                        this._smartMobileContext.OutletVendorContribution.InsertAllOnSubmit(outletVenderList);
                        this._smartMobileContext.SubmitChanges();
                    }
                    
                }                
            }
            else
            {
                List<OutletVendorContribution> outletVenderList = new List<OutletVendorContribution>();
                foreach (var item in this.OutletVendorContributionObserable)
                {
                    OutletVendorContribution vender = new OutletVendorContribution
                    {
                        ID_PK = item.ID_PK,
                        CustomerKey = item.CustomerKey,
                        OutletVendorKey = item.OutletVendorKey,
                        ContributionAmt = item.ContributionAmt
                    };
                    outletVenderList.Add(vender);
                }

                this._smartMobileContext.OutletVendorContribution.InsertAllOnSubmit(outletVenderList);
                this._smartMobileContext.SubmitChanges();
            }
        }

        private string GetOutletVendorName(string vendorCode)
        {
            return this._smartMobileContext.OutletVendor.Where(o => o.OutletVendorKey == vendorCode).Select(o => o.OutletVendorName).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }
        #endregion
    }
}
